/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eighteen12.web.controller;

import com.eighteen12.web.Controller;
import com.eighteen12.web.controller.vo.CurrencyVo;
import com.eighteen12.web.controller.vo.Utilities;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 *
 * @author Arif
 */
public class CurrenciesMandiri implements Controller<CurrencyVo> {

    public CurrencyVo execute(HttpServletRequest request) {
        CurrencyVo result = new CurrencyVo();
        result.bankCode = "Mandiri";
        result.bankName = "Bank Mandiri";
        try {
            Document doc = Jsoup.connect("http://www.bankmandiri.co.id/resource/kurs.asp?row=2").get();
            Elements elements = doc.getElementsByClass("tbl-view").first().getElementsByTag("tr").get(1).getElementsByTag("td");
            result.dollarJual = Double.parseDouble(elements.get(4).text().replaceAll("\\.", "").replaceAll(",", "")) / 100;
            result.dollarBeli = Double.parseDouble(elements.get(2).text().replaceAll("\\.", "").replaceAll(",", "")) / 100;
            result.lastUpdate = new Date();
            new Utilities().storeToDb(result);
        } catch (IOException ex) {
            Logger.getLogger(CurrenciesMandiri.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

}
